/*
 * @Autor: Ruohong Yu
 * @Date: 2023-03-31 15:56:52
 * @Description: file content
 * @FilePath: \ebookd:\program\vsc\ebook\node-ebook\router_handler\shelf.js
 */
const config = require('../bin/const')

function handleData(data,bookstatus){
  if(!data.cover.startsWith('http://')){
    data['cover']=`${config.resUrl}/img${data.cover}`
  }
  data['selected']=false
  data['cache']=false
  data['groupname']=bookstatus.groupname
  if(bookstatus.private===0||bookstatus.private==='0'){
    data['private']=false
  }else{
    data['private']=true
  }
  if(bookstatus.haveRead===0||bookstatus.haveRead==='0'){
    data['haveRead']=0
  }else{
    data['haveRead']=1
  }
  return data
}

function sqlBookinfo(results,i,list,groupList){
  return new Promise((resolve,reject)=>{
    let sql='select * from book where fileName=?'
    config.db.query(sql,results[i].bookname,(err,bookinfo)=>{
      if(err) reject(err)
      if(bookinfo.length===1){
        if(groupList.indexOf(results[i].groupname)===-1){
          groupList.push(results[i].groupname)
        }
        let book=handleData(bookinfo[0],results[i])
        list.push(book)
        resolve(list,groupList)
      }
    })
  })
}

function getListAndGroupList(results){
  return new Promise(async (resolve,reject)=>{
    let list=[],groupList=[]
    for(var i=0;i<results.length;i++){
      await sqlBookinfo(results,i,list,groupList).then((res)=>{
        // console.log(res)
      })
    }
    resolve({list:list,groupList:groupList})
  })
}

function makeShelfList(groupList,list){
  let finalList=[]
  for(var i=0;i<groupList.length;i++){
    if(groupList[i]==='0'){
      list.find(function(val,inde,err){
        if(val.groupname==='0'){
          val.type=1
          finalList.push(val)
        }
      })
    }else{
      let soe=[]
      list.find(function(val,inde,err){
        if(val.groupname===groupList[i]){
          val.type=1
          soe.push(val)
        }
      })
      let one={}
      one.itemList=soe
      one.selected=false
      one.title=groupList[i]
      one.type=2
      finalList.push(one)
    }
  }
  return finalList
}

exports.getShelfBook= (req,res)=>{
  const getinfo = req.body
  const sql = 'select * from user_shelf where username=?'
  let finalList=[]
  config.db.query(sql, getinfo.username, async (err, res2) => {
    if (err) return res.send({ status: 1, message: err.message })
    try{
      await getListAndGroupList(res2).then((val)=>{
        finalList=makeShelfList(val.groupList,val.list)
        res.send({status:0,message:'获取书架列表成功',bookList:finalList})
      })
    }
    catch(err){
      res.send({status:1,message:err.message})
    }
  })
}

exports.addShelfBook=(req,res)=>{
  const addinfo = req.body
  const sql = 'insert into user_shelf set ?'
  config.db.query(sql, { bookname: addinfo.bookname, username: addinfo.username, groupname:addinfo.groupname, private:addinfo.private}, (err, results) => {
    if (err) return res.send({ status: 1, message: err.message })
    if (results.affectedRows !== 1) return res.send({ status: 1, message: '加入书架失败' })
    res.send({ status: 0, message: '加入书架成功！' })
  })
}

exports.delShelfBook=(req,res)=>{
  const delinfo = req.body
  const sql = 'delete from user_shelf where username=? and bookname in (?)'
  config.db.query(sql, [delinfo.username,delinfo.bookname], (err, results) => {
    if (err) return res.send({ status: 1, message: err.message })
    res.send({ status: 0, message: '移除书架成功！' })
  })
}

exports.setPrivate=(req,res)=>{
  const setinfo = req.body
  if(setinfo.private){
    setinfo.private=1
  }else{
    setinfo.private=0
  }
  for(var i=0;i<setinfo.booklist.length;i++){
    const sql = 'update user_shelf set private=? where username=? and bookname=?'
    config.db.query(sql,[setinfo.private,setinfo.username,setinfo.booklist[i].fileName],(err, results) => {
      if (err) return res.send({ status: 1, message: err.message })
      if (results.affectedRows !== 1) return res.send({ status: 1, message: '私密阅读设置失败' })
      res.send({ status: 0, message: '私密阅读设置成功！' })
    })
  }
}

exports.setHaveRead=(req,res)=>{
  const setinfo = req.body
  const sql = 'select * from user_read where username=? and bookname=?'
  config.db.query(sql,[ setinfo.username, setinfo.bookname ],(err, results) => {
    if (err) return res.send({ status: 1, message: err.message })
    if (results.length >= 1) return res.send({ status: 1, message: '该条数据已记录' })
    const sql = 'insert into user_read set ?'
    config.db.query(sql,{ username:setinfo.username, bookname:setinfo.bookname },(err, results) => {
      if (err) return res.send({ status: 1, message: err.message })
      if (results.affectedRows === 1) return res.send({ status: 1, message: '已阅读设置成功' })
      res.send({ status: 0, message: '已阅读设置失败' })
    })
  })
  
}

exports.removeGroup=(req,res)=>{
  const setinfo = req.body
  const sql = 'update user_shelf set groupname=? where username=? and bookname=?'
  for(var i=0;i<setinfo.booklist.length;i++){
    config.db.query(sql,[setinfo.groupname,setinfo.username,setinfo.booklist[i].fileName],(err, results) => {
      if (err) return res.send({ status: 1, message: err.message })
      if (results.affectedRows !== 1) return res.send({ status: 1, message: '移动分组成功' })
      res.send({ status: 0, message: '移动分组成功' })
    })
  }
}